using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Lesson8_Exercises : MonoBehaviour
{
    private Rigidbody body;
    private Vector3 dir;
    public Bullet bullet;

    Lesson8Input_Exercises input;
    void Start()
    {
        body = this.GetComponent<Rigidbody>();
        input = new Lesson8Input_Exercises();
        input.Enable();
        input.Action.Jump.performed += Jump;
        input.Action.Fire.performed += Fire;
    }

    private void Jump(InputAction.CallbackContext context)
    {
        body.AddForce(Vector3.up * 200);
    }

    private void Fire(InputAction.CallbackContext context)
    {
        //鼠标位置的射线检测
        RaycastHit info;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()), out info))
        {
            //得到子弹飞出去的向量
            Vector3 point = info.point;
            point.y = this.transform.position.y;
            Vector3 dir = point - this.transform.position;
            //创建子弹飞出去
            Instantiate(bullet, this.transform.position, Quaternion.LookRotation(dir));
        }
    }

    // Update is called once per frame
    void Update()
    {
        dir = input.Action.Move.ReadValue<Vector2>();
        dir.z = dir.y;
        dir.y = 0;
        body.AddForce(dir * 10);
    }

    void OnDestroy()
    {
        input.Action.Jump.performed -= Jump;
        input.Action.Fire.performed -= Fire;
    }
}
